package com.example.exam.conf.security;

import com.example.exam.utils.Md5Utils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author zhang
 */
@Log4j
public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();

    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String salt = encodedPassword.substring(encodedPassword.length() - 6);
        rawPassword = Md5Utils.toHex(Md5Utils.md5(rawPassword.toString(), salt));
        encodedPassword = encodedPassword.substring(0, encodedPassword.length() - 6);
        if (rawPassword != null) {
            return rawPassword.equals(encodedPassword);
        }
        return false;
    }
}
